/**
 * Created by Administrator on 2017/9/28.
 */

$(document).ready(function () {

    var id = locationSearcher('id');
    var detailTpl = $('#banner-detail-template').html();
    var detailCmp = Handlebars.compile(detailTpl);

    var flag =0,
        num = 0,
        maxNum = 0,
        start = 0,
        length = 4,
        range = 200,
        totalHeight = 0;

    getDetail();

    $(window).on('scroll',function () {
        //滚动条距离顶部的距离
        var scrollPos = $(window).scrollTop();
        totalHeight = parseFloat($(window).height()) + parseFloat(scrollPos);
        if (num >= maxNum){
            return false;
        }
        if (($(document).height()-range) <= totalHeight && num < maxNum){
            getDetail();
        }
    });

    function getDetail() {
        if (flag){
            return ;
        }
        $('.no-info').hide();
        $('.loading').show();
        flag = 1;
        $.ajax({
            url:C.interface.bannerJump,
            type:'get',
            dataType:'json',
            data:{
                id:id,
                start:start,
                length:length
            },
            success:function (response) {
                if (response.result === 'success'){
                    $('.loading').hide();
                    num++;
                    var data = response.data;
                    var imgSrc = data.banner.url;
                    $('.banner-img').attr('src',imgSrc);
                    maxNum = data['count'];
                    start = num * length;
                    if (maxNum == 0){
                        $('.no-info').show();
                    }
                    if (data.products.length >0){
                        $('#banner-detail-box').append(detailCmp(data));
                        new auiLazyload({
                            errorImage:'../img/errorimg.png'
                        });
                    }else {
                        $('.no-info').show();
                        num = maxNum +1;
                    }
                }
                flag = 0;
            }
        })
    }
});